<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="菜品分组列表"
      row-key="id"
      :columns="columns"
      :request-api="getGoodsGroupListAPI"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', defaultData)">
          新增分组
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="View" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <GoodsGroupDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="goodsCategory">
import { ref, reactive, onMounted } from "vue";
import { Goods } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import GoodsGroupDialog from "@/views/goods/components/GoodsGroupDialog.vue";
import { DialogProps } from "@/views/goods/components/GoodsGroupDialog.vue";
import { CirclePlus, Delete, EditPen, View } from "@element-plus/icons-vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import {
  getGoodsGroupListAPI,
  deleteFoodCategory,
  editFoodCategory,
  addFoodCategory,
  deleteGoodsGroupAPI
} from "@/api/modules/goods";
import { formatDate } from "@/utils/date-formatter";

onMounted(() => {
  console.log(formatDate(0, "start", "yyyy-MM-dd"), formatDate(-30, "end", "yyyy-MM-dd"));
});

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// const initParam = reactive({ listType: 0 });

const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};

// 新建加料默认参数
const defaultData = {
  name: "",
  desc: "",
  foodspecIds: [],
  detailList: []
};

// 表格配置项
const columns: ColumnProps<Goods.ResGoodsCategoryList>[] = [
  { type: "index", label: "#", width: 60 },
  { sortable: true, prop: "name", label: "分组名称", align: "left", minWidth: 120, search: { el: "input" } },
  { sortable: true, prop: "count", label: "关联菜品数量", align: "left", minWidth: 140 },
  { sortable: true, prop: "desc", label: "描述", align: "left", minWidth: 140 },
  { sortable: true, prop: "createTime", label: "创建时间", align: "left", minWidth: 140 },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

// 删除用户信息
const deleteAccount = async (params: Goods.ResGoodsCategoryList) => {
  await useHandleData(deleteGoodsGroupAPI, { id: params.id }, `删除【${params.name}】分组`);
  proTable.value?.getTableList();
};

// 打开 dialog(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof GoodsGroupDialog> | null>(null);
const openDialog = (title: string, row: any) => {
  console.log(row);

  const params: DialogProps = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? addFoodCategory : title === "编辑" ? editFoodCategory : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
